package com.yenlien.traceyun.produce.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.entity.ProcessMaterials;
import com.yenlien.traceyun.business.entity.ProcessPlan;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.produce.service.ProcessService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 生产控制contr
 */
@RestController
@RequestMapping("/process")
public class ProcessController extends BaseController {
    @Autowired
    private ProcessService processService;

    /**
     * 新增
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody JSONObject params) {
        ProcessPlan processPlan = params.getJSONObject("processplan").toJavaObject(ProcessPlan.class);
        List<ProcessMaterials> materialsList = params.getJSONArray("materials").toJavaList(ProcessMaterials.class);
        processService.save(processPlan, materialsList);
        return success("success");
    }

    /**
     * 查询生产计划
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        String searchName = params.getString("searchName");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        if (page > 0) {
            Integer page1 = page - 1;
            return success(processService.select(page1, searchName, companyId));
        } else {
            return fail("页面加载错误");
        }
    }

    /**
     * 搜索栏
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    public Object search(@RequestBody JSONObject params) {
        String tradeName = params.getString("tradeName");
        if (tradeName.length() > 0) {
            return success(processService.search(tradeName));
        } else {
            return fail("信息未存在");
        }
    }

    /**
     * 根据id查询基本信息和原料辅料
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(processService.selectId(id));
        } else {
            return fail("物资不存在");
        }
    }

    /**
     * 根据id修改基本信息和原料辅料信息
     *
     * @param params
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody JSONObject params) {
        ProcessPlan processPlan = params.getJSONObject("processplan").toJavaObject(ProcessPlan.class);
        List<ProcessMaterials> materialsList = params.getJSONArray("materials").toJavaList(ProcessMaterials.class);
        processService.update(processPlan, materialsList);
        return success("success");
    }

    /**
     * 根据id删除基本信息和原料辅料信息
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody JSONObject params) {
        ProcessPlan processPlan = params.getJSONObject("processplan").toJavaObject(ProcessPlan.class);
        processService.delete(processPlan);
        return success("success");
    }
}
